package com.zhangdi.mgsync.core.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;

public class ClassUtils {
    private static final Logger log = LoggerFactory.getLogger(ClassUtils.class);
    private static final ClassUtils.ClassLoaderAccessor THREAD_CL_ACCESSOR = new ClassUtils.ExceptionIgnoringAccessor() {
        protected ClassLoader doGetClassLoader() {
            return Thread.currentThread().getContextClassLoader();
        }
    };
    private static final ClassUtils.ClassLoaderAccessor CLASS_CL_ACCESSOR = new ClassUtils.ExceptionIgnoringAccessor() {
        protected ClassLoader doGetClassLoader() {
            return ClassUtils.class.getClassLoader();
        }
    };
    private static final ClassUtils.ClassLoaderAccessor SYSTEM_CL_ACCESSOR = new ClassUtils.ExceptionIgnoringAccessor() {
        protected ClassLoader doGetClassLoader() {
            return ClassLoader.getSystemClassLoader();
        }
    };

    public ClassUtils() {
    }

    public static InputStream getResourceAsStream(String name) {
        InputStream is = THREAD_CL_ACCESSOR.getResourceStream(name);
        if (is == null) {
            if (log.isTraceEnabled()) {
                log.trace("Resource [" + name + "] was not found via the thread context ClassLoader.  Trying the current ClassLoader...");
            }

            is = CLASS_CL_ACCESSOR.getResourceStream(name);
        }

        if (is == null) {
            if (log.isTraceEnabled()) {
                log.trace("Resource [" + name + "] was not found via the current class loader.  Trying the system/application ClassLoader...");
            }

            is = SYSTEM_CL_ACCESSOR.getResourceStream(name);
        }

        if (is == null && log.isTraceEnabled()) {
            log.trace("Resource [" + name + "] was not found via the thread context, current, or system/application ClassLoaders.  All heuristics have been exhausted.  Returning null.");
        }

        return is;
    }

    private abstract static class ExceptionIgnoringAccessor implements ClassUtils.ClassLoaderAccessor {
        private ExceptionIgnoringAccessor() {
        }

        public Class loadClass(String fqcn) {
            Class clazz = null;
            ClassLoader cl = this.getClassLoader();
            if (cl != null) {
                try {
                    clazz = cl.loadClass(fqcn);
                } catch (ClassNotFoundException var5) {
                    if (ClassUtils.log.isTraceEnabled()) {
                        ClassUtils.log.trace("Unable to load clazz named [" + fqcn + "] from class loader [" + cl + "]");
                    }
                }
            }

            return clazz;
        }

        public InputStream getResourceStream(String name) {
            InputStream is = null;
            ClassLoader cl = this.getClassLoader();
            if (cl != null) {
                is = cl.getResourceAsStream(name);
            }

            return is;
        }

        protected final ClassLoader getClassLoader() {
            try {
                return this.doGetClassLoader();
            } catch (Throwable var2) {
                if (ClassUtils.log.isDebugEnabled()) {
                    ClassUtils.log.debug("Unable to acquire ClassLoader.", var2);
                }

                return null;
            }
        }

        protected abstract ClassLoader doGetClassLoader() throws Throwable;
    }

    private interface ClassLoaderAccessor {
        Class loadClass(String var1);

        InputStream getResourceStream(String var1);
    }
}